<?php

namespace app\controllers;
use srvs\ContentDetailService;
use srvs\ContentService;
use srvs\MenuService;

class SystemController extends BaseController
{
    public function actionInit(){
        $MenuService=new MenuService();
        $ContentService=new ContentService();
        $ContentDetailService=new ContentDetailService();
        try{
            $datalist=$MenuService->getAll();
            foreach($datalist as $data){
                $MenuService->delete($data["objectId"]);
            }
            $datalist=$ContentService->getAll();
            foreach($datalist as $data){
                $ContentService->delete($data["objectId"]);
            }
            $datalist=$ContentDetailService->getAll();
            foreach($datalist as $data){
                $ContentDetailService->delete($data["objectId"]);
            }
        }
        catch(\Exception $ex){
            echo $ex->getMessage()."<br>";
        }
        $data=["menu_name"=>"sdgk","menu_sort"=>0,"menu_type"=>0,"parent_id"=>""];
        $MenuService->add($data);

        $data=["menu_name"=>"cpzx","menu_sort"=>1,"menu_type"=>1,"parent_id"=>""];
        $object_id_cpzx=$MenuService->add($data);
        $data=["menu_name"=>"cpfl1","menu_sort"=>1,"menu_type"=>1,"parent_id"=>$object_id_cpzx];
        $child_id1=$MenuService->add($data);
        for($i=1;$i<=21;$i++){
            $object_id_cnt1=$ContentService->add(["title"=>"2016中国·武汉劳动关系年度高峰论坛圆满落幕！".$i,"menu_id"=>$child_id1,"image_url"=>"","description"=>"",
                "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
            $ContentDetailService->add(["content_id"=>$object_id_cnt1,"detail"=>"cpfl1<br><b>Test</b>".$i]);
        }

        $data=["menu_name"=>"xwdt","menu_sort"=>2,"menu_type"=>0,"parent_id"=>""];
        $object_id_xwdt=$MenuService->add($data);

        for($i=1;$i<=50;$i++){
            $object_id_cnt1=$ContentService->add(["title"=>"title".$i,"menu_id"=>$object_id_xwdt,"image_url"=>"","description"=>"",
                "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
            $ContentDetailService->add(["content_id"=>$object_id_cnt1,"detail"=>"xwdt<br><b>Test</b>".$i]);
        }


        $data=["menu_name"=>"qyxc","menu_sort"=>3,"menu_type"=>1,"parent_id"=>""];
        $MenuService->add($data);

        $data=["menu_name"=>"lxwm","menu_sort"=>4,"menu_type"=>0,"parent_id"=>""];
        $object_id_lxwm=$MenuService->add($data);
        $object_id_cnt1=$ContentService->add(["title"=>"title","menu_id"=>$object_id_lxwm,"image_url"=>"","description"=>"",
            "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
        $ContentDetailService->add(["content_id"=>$object_id_cnt1,"detail"=>"<h1>Hello World</h1>"]);

        $data=["menu_name"=>"lxdh","menu_sort"=>0,"menu_type"=>0,"parent_id"=>$object_id_lxwm];
        $child_id1=$MenuService->add($data);

        $object_id_cnt1=$ContentService->add(["title"=>"title","menu_id"=>$child_id1,"image_url"=>"","description"=>"",
            "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
        $ContentDetailService->add(["content_id"=>$object_id_cnt1,"detail"=>"lxdh1"]);

        $data=["menu_name"=>"lxdz","menu_sort"=>1,"menu_type"=>0,"parent_id"=>$object_id_lxwm];
        $child_id2=$MenuService->add($data);

        $object_id_cnt1=$ContentService->add(["title"=>"title1","menu_id"=>$child_id2,"image_url"=>"","description"=>"",
            "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
        $ContentDetailService->add(["content_id"=>$object_id_cnt1,"detail"=>"$object_id_cnt1"]);

        $object_id_cnt2=$ContentService->add(["title"=>"title2","menu_id"=>$child_id2,"image_url"=>"","description"=>"",
            "keywords"=>"","status"=>0,"createdBy"=>"","hits"=>0,"is_top"=>false]);
        $ContentDetailService->add(["content_id"=>$object_id_cnt2,"detail"=>"$object_id_cnt2"]);

        echo "OK";
    }

    public function actionEnv(){
        echo YII_ENV;
    }

    public function actionLogin()
    {
        \LeanCloud\User::logIn("yzh2","888");
    }

    public function actionLoginOut(){
        \LeanCloud\User::logOut();
    }

    public function actionGetUser(){
        $user = \LeanCloud\User::getCurrentUser();
        if ($user) {
            echo $user->getUsername();
        } else {
            echo "no login";
        }
    }

    public function actionUpload(){
        if(!\Yii::$app->request->post()){
            return $this->render('index');
        }
        if (isset($_FILES["attachmentUploader"]) && $_FILES["attachmentUploader"]["size"] != 0) {
            $file = \LeanCloud\File::createWithLocalFile(
                $_FILES["attachmentUploader"]["tmp_name"],
                $_FILES["attachmentUploader"]["type"]
            );
            echo $file->getUrl();
        } else {
            echo ("请选择一个文件");
        }
    }
}